const { default: Toast } = require("../../../../component/vant/toast/toast.js");

import Dialog from '../../../../component/vant/dialog/dialog';
// pagesMer/pages/couponStore/add/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"),
common_Js = require("../../../../component/common.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponStore:{},
    storeQr:{},
    store_qr_id:0,
    isLoading:true,
    netWork:false,
    totalCount:0,
    submitState: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.data.store_qr_id = options.id;
    }
    this.onHttpInit();
  },

  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/bingge/coupon/store/qr/detail', {
      store_qr_id: that.data.store_qr_id,
    }, function (res) {
      if (res.data.code == 0) {
        that.data.store = res.data.data.store;
        that.setData({
          storeQr: res.data.data.storeQr,
          store: res.data.data.store,
          totalCount: res.data.data.totalCount,
        })
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  },
  goCoupon(){
    var that = this;
    var message = "是否领取该卡券？";
    Dialog.confirm({
        title: '卡券领取',
        message
      }).then(() => {
        that.onHttpSubmit();
      })
      .catch(() => {
        Dialog.close();
      });
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpSubmit: function () {
    var that = this
    fetchApi.httpPost('/bingge/coupon/sale', {
      store_qr_id: that.data.store_qr_id,
      coupon_store_id: that.data.store.id,
    }, function (res) {
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.switchTab({
            url: '/pages/home/home',
          })
        }, 300)
      }else{
        Toast(res.data.msg);
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  },
})